﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CompanyBussiness;

namespace Website.admin.Control
{
    public partial class cEditNick : System.Web.UI.UserControl
    {
        NewsManagement newsMana = new NewsManagement();
        LanguagesManagement languageMana = new LanguagesManagement();
        ServicesBasic serviceBasic = new ServicesBasic();
        CompanyBussiness.Contact contact;
        CompanyBussiness.Language language;
        CompanyBussiness.New news;
        CheckValid cv = new CheckValid();
        string strFileName = "";
        bool AddOrUp = false;
        int STRID;
        string error = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Cookies[CONST.COOKIES_LANGUAGE] == null)
            {
                Session[CONST.SESSION_LANGUAGE] = "";
                language = languageMana.GetLanguageByCode("vi-VN");
            }
            else
            {
                Session[CONST.SESSION_LANGUAGE] = Request.Cookies[CONST.COOKIES_LANGUAGE][CONST.COOKIES_LANGUAGE_ID];
                language = languageMana.GetLanguageByID(int.Parse(Session[CONST.SESSION_LANGUAGE].ToString()));
            }
        }

        protected bool GetData()
        {
            if (txtName.Text.Trim() != "")
            {
                contact.ContactName = txtName.Text;
            }
            else
            {
                return false;
            }

            if (txtNick.Text.Trim() != "")
            {

                contact.ContactNick = txtNick.Text;
            }
            else
            {
                return false;
            }


            return true;
        }

        protected void LoadForm()
        {
            try
            {
                txtName.Text = contact.ContactName;

                if (contact.ContactNick != null)
                    txtNick.Text = contact.ContactNick;
                else
                    txtNick.Text = "";

            }
            catch (Exception)
            {
                //fail.Visible = true;
                //lbError.Text = "Có lỗi trong khi Load dữ liệu . Bạn hãy kiểm tra lại.";
            }

        }

        protected void FormatForm()
        {
            try
            {
                if (AddOrUp == false)
                {
                    txtNick.Text = "";
                    txtName.Text = "";

                }
                else
                {
                    this.LoadForm();
                }
            }
            catch (Exception)
            { }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            contact = serviceBasic.GetContactByID(STRID);

            if (GetData() == false)
                return;

            serviceBasic.UpdateNick((int)contact.ContactID, contact.ContactName, contact.ContactNick);

            Response.Redirect("Administrator.aspx?" + CONST.ACTION + "=Settings");
            //success.Visible = true;
            //lbSuccess.Text = "Bạn đã cập thành công !";
            //fail.Visible = false;

            this.LoadForm();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.FormatForm();
        }
    }
}